/*
 * Copyright (c) 2010-2013 Ruben Chakhmakhchyan. All rights reserved.
 * 
 * This file contains Original Code and/or Modifications of Original Code
 * as defined in and that are subject to the Eclipse Public License
 * Version 1.0 (the 'License'). You may not use this file except in
 * compliance with the License. Please obtain a copy of the License at
 * http://opensource.org/licenses/EPL-1.0 and read it before using this file.
 */

#include "plugin_model.h"
#include "scripting.h"


ScriptableImageIndexer::ScriptableImageIndexer( NPP npp )
    : NPObjectBase( npp ),
    m_model( NULL )
{
    m_npidList.reserve( 64 );
}

ScriptableImageIndexer::~ScriptableImageIndexer()
{
}

bool ScriptableImageIndexer::construct( const NPVariant *args, uint32_t argCount, NPVariant *result )
{
    NPObject *obj = ::NPN_CreateObject( m_npp, GET_NPOBJECT_CLASS( ScriptableImageIndexer ) );
    if ( !obj )
        return false;

    NPVariant& resultValue = *result;

    OBJECT_TO_NPVARIANT( obj, resultValue );

    return true;
}

bool ScriptableImageIndexer::hasMethod( NPIdentifier name )
{
    return false;
}

bool ScriptableImageIndexer::hasProperty( NPIdentifier name )
{
    bool result = false;

    if ( name == npid.length || name == npid.count )
    {
        result = true;
    }
    else
    {
        int count = m_model->getImageCount();

        for ( int i = m_npidList.count(); i <= count; i++ )
        {
            m_npidList.add( ::NPN_GetIntIdentifier( i ) );
        }

        result = m_npidList.contains( name );
    }

    return result;
}

bool ScriptableImageIndexer::getProperty( NPIdentifier name, NPVariant *result )
{
    bool returnValue = false;
    NPVariant& resultValue = *result;
    VOID_TO_NPVARIANT( resultValue );

    if ( name == npid.length || name == npid.count )
    {
        INT32_TO_NPVARIANT( m_model->getImageCount(), resultValue );
        returnValue = true;
    }
    else if ( name == npid.current )
    {
        int selectedImageIndex = m_model->getSelectedImageIndex();

        getProperty( ::NPN_GetIntIdentifier( selectedImageIndex ), result );

        returnValue = true;
    }
    else
    {
        int index = m_npidList.indexOf( name );

        if ( index >= 0 && index < m_model->getImageCount() )
        {
            const PluginImage* image = m_model->getImage( index );
            void* tag = image->getTag();

            if ( tag == NULL )
            {
                NPObject *obj = ::NPN_CreateObject( m_npp, GET_NPOBJECT_CLASS( ScriptableImage ) );

                ScriptableImage* scriptableImage = ( ScriptableImage* ) obj;
                scriptableImage->setReference( image );
                tag = obj;

                PluginImage* pluginImage = const_cast<PluginImage*>( image );
                pluginImage->setTag( ( void* ) tag );
                pluginImage->setEventHandler( scriptableImage );
            }

            ::NPN_RetainObject( ( NPObject* ) tag );

            OBJECT_TO_NPVARIANT( ( NPObject* ) tag, resultValue );

            returnValue = true;
        }
    }

    return returnValue;
}

bool ScriptableImageIndexer::invoke( NPIdentifier name, const NPVariant *args, uint32_t argCount, NPVariant *result )
{
    return false;
}
